home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0050_Low Level File Read Obj.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-15  |  8KB  |  310 lines

  1. {*************************************************************************
  2.  *                    LOW LEVEL FILE READING OBJECT                      *
  3.  *                                                                       *
  4.  *                      Copyright 1992 Tom Clancy                        *
  5.  *                                                                       *
  6.  *   Description:                                                        *
  7.  *                                                                       *
  8.  *        This library allows you to create a file of any type record    *
  9.  *   by passing in the record size.  You must also pass in a record of   *
  10.  *   the same type that the object has been initialized with so that     *
  11.  *   you don't get errors when reading and writing.                      *
  12.  *                                                                       *
  13.  *        There is no internal buffering, but the routines are fairly    *
  14.  *   fast and because each file is actually an object, you can create    *
  15.  *   higher level objects of this object type that allow more            *
  16.  *   flexibility, such as indexing and sorting.                          *
  17.  *                                                                       *
  18.  *************************************************************************}
  19.  
  20. {$I-}   {Turn off I/O checking}
  21. {$S-}   {Turn off Stack checking}
  22. {$R-}   {Turn off Range checking}
  23. {$V-}   {No strict VAR string checking allowed here!}
  24.  
  25. Unit FileRead;
  26.  
  27. Interface
  28.  
  29. Const
  30.  
  31.    Open       = 1;
  32.    Create     = 2;
  33.    OpenCreate = 3;
  34.  
  35. Type
  36.  
  37.    TFreadPtr = ^TFreadObj;
  38.    TFreadObj = Object
  39.      Constructor Init(fn : string; mode:integer; recsize : longint);
  40.      Destructor  Done;
  41.  
  42.      { random access methods. }
  43.      Procedure   ReadRec(var frec; fpos:longint);  virtual;
  44.      Procedure   WriteRec(var frec; fpos:longint); virtual;
  45.  
  46.      { sequential access methods. }
  47.      Procedure   AppendRec(var frec);
  48.      Procedure   ReadNext(var frec);
  49.      Procedure   ReadPrevious(var frec);
  50.      Procedure   ReadCurrent(var frec);
  51.  
  52.      { various file modification methods. }
  53.      Procedure   EraseFile;
  54.      Function    RenameFile(fn:string):boolean;
  55.  
  56.      { miscellaneous functions and error flag functions. }
  57.      Procedure   Rewind;
  58.      Function    NumRecs     : Longint;
  59.      Function    GetFilename : String;
  60.      Function    GetCurrent  : Longint;
  61.      Function    OpenError   : boolean;
  62.      Function    ReadError   : boolean;
  63.      Function    WriteError  : boolean;
  64.  
  65.    private
  66.      Ifile       : File;     {file variable}
  67.      Rsize       : Longint;  {the internal record size}
  68.      FileName    : String;   {physical file name}
  69.      Oerror,                 {open error flag}
  70.      Rerror,                 {read error flag}
  71.      Werror      : Boolean;  {write error flag}
  72.      Current     : Longint;  {current file pointer location}
  73.  
  74.      { methods used internally.  No access allowed! }
  75.      Procedure   OpenFile;
  76.      Procedure   CreateFile;
  77.      Procedure   CloseFile;
  78.    end;
  79.  
  80. Function Exist(fn:string):Boolean;
  81.  
  82. Implementation
  83.  
  84. uses
  85.   Dos;
  86.  
  87. { Pass in a string which contains a file name to see if that file exists.}
  88. Function Exist(fn:string):Boolean;
  89. Var
  90.    DirInfo : SearchRec;
  91. Begin
  92.   FindFirst(fn,Archive,DirInfo);
  93.   Exist:=DosError=0;
  94. End;
  95.  
  96. {
  97.     Initialize the object.
  98.  
  99.     Fn    = File name
  100.     Mode  = Open, Create, or OpenCreate
  101.       - Open will try to open the file.  An error is set if the file does not
  102.         exist.
  103.       - Create will create the file regardless if it's there or not.
  104.       - OpenCreate will attemp to open the file first, then create it if it's
  105.         not there.
  106.     RecSize = The size of the records that the file will contain.
  107.       - Use Sizeof(Rec) for safety.
  108. }
  109. Constructor TFreadObj.Init(fn:string; mode:integer; recsize:longint);
  110. Begin
  111.   Oerror:=true;
  112.   Rerror:=false;
  113.   Werror:=false;
  114.   Rsize:=recsize;
  115.   FileName := fn;
  116.   Assign(Ifile,FileName);
  117.   case mode of
  118.     Open       : openfile;
  119.     Create     : createfile;
  120.     OpenCreate :
  121.       begin
  122.         OpenFile;
  123.         if Oerror then
  124.           CreateFile;
  125.       end;
  126.   end;
  127. End;
  128.  
  129. { Close the file when disposing object. }
  130. Destructor TFreadObj.done;
  131. begin
  132.   CloseFile;
  133. end;
  134.  
  135. { Open the file.  Set an error if it could not open. }
  136. Procedure TFreadObj.OpenFile;
  137. Begin
  138.   if Exist(FileName) then
  139.   begin
  140.     Oerror:=false;
  141.     Reset(Ifile,Rsize);
  142.     Current:=0;
  143.   end
  144.   else
  145.     Oerror:=true;
  146. End;
  147.  
  148. { Create a new file, zeroing out an existing file.}
  149. Procedure TFreadObj.CreateFile;
  150. Begin
  151.   Rewrite(Ifile,Rsize);
  152.   Current:=0;
  153.   Oerror:=Ioresult<>0;
  154. end;
  155.  
  156. { Close the file only if it has been successfully opened.}
  157. Procedure TFreadObj.CloseFile;
  158. Begin
  159.   if not Oerror then
  160.   begin
  161.     Close(Ifile);
  162.     Oerror:=true;
  163.   end;
  164. End;
  165.  
  166. { Will erase the file.}
  167. Procedure TFreadObj.EraseFile;
  168. Begin
  169.   if not Oerror then
  170.   begin
  171.     CloseFile;
  172.     Erase(Ifile);
  173.   end;
  174. End;
  175.  
  176. { Renames the file.}
  177. Function TFreadObj.RenameFile(fn:string):Boolean;
  178. Var
  179.   Temp : Longint; {Save the current file pointer}
  180. Begin
  181.   CloseFile;
  182.   FileName:=fn;
  183.   Rename(Ifile,FileName);
  184.   if ioresult=0 then
  185.   begin
  186.     Temp:=Current;
  187.     Assign(Ifile,FileName);
  188.     OpenFile;
  189.     Current:=Temp;
  190.   end;
  191.   RenameFile := not Oerror;
  192. end;
  193.  
  194. { Rewinds the file pointer back to the beginning.}
  195. Procedure TFreadObj.Rewind;
  196. Begin
  197.   if not Oerror then
  198.   begin
  199.     Seek(Ifile,0);
  200.     Current:=0;
  201.   end;
  202. end;
  203.  
  204.  
  205. Function TFreadObj.OpenError:Boolean;
  206. Begin
  207.   OpenError:=Oerror;
  208. End;
  209.  
  210. Function TFreadObj.ReadError:Boolean;
  211. Begin
  212.   ReadError:=Rerror;
  213. End;
  214.  
  215. Function TFreadObj.WriteError:Boolean;
  216. Begin
  217.   WriteError:=Werror;
  218. End;
  219.  
  220. { Reads a record from the file at location FPOS.  Returns the record in
  221.   Frec.}
  222. Procedure TFreadObj.ReadRec(var frec; fpos:longint);
  223. Var
  224.   numread : word;
  225. Begin
  226.   Rerror:=false;
  227.   if not Oerror then
  228.   begin
  229.     Seek(Ifile,fpos);
  230.     if ioresult<>0 then
  231.       Rerror:=true
  232.     else
  233.     begin
  234.       Blockread(Ifile,frec,1,numread);
  235.       if (numread<>1) or (ioresult<>0) then
  236.         Rerror:=true
  237.       else
  238.         Current:=fpos;
  239.     end;
  240.   end;
  241. End;
  242.  
  243. { Writes a record to the file at location Fpos.}
  244. Procedure TFreadObj.WriteRec(var frec; fpos:longint);
  245. Var
  246.   numwritten : word;
  247.   i:integer;
  248. Begin
  249.   Werror:=false;
  250.   if not Oerror then
  251.   begin
  252.     Seek(Ifile,fpos);
  253.     if Ioresult<>0 then
  254.       Werror:=true
  255.     else
  256.     begin
  257.       Blockwrite(Ifile,frec,1,numwritten);
  258.       if (numwritten<>1) or (ioresult<>0) then
  259.         Werror:=true
  260.       else
  261.         Current:=fpos;
  262.     end;
  263.   end;
  264. End;
  265.  
  266. { Appends a record to the end of the file.}
  267. Procedure TFreadObj.AppendRec(var frec);
  268. Begin
  269.   WriteRec(frec,NumRecs);
  270. End;
  271.  
  272. { Reads the next record from the file, allowing sequential access.}
  273. Procedure TFreadObj.ReadNext(var frec);
  274. Begin
  275.   ReadRec(frec,Current+1);
  276. End;
  277.  
  278. { Reads the previous record from the file. }
  279. Procedure TFreadObj.ReadPrevious(var frec);
  280. Begin
  281.   ReadRec(frec,Current-1);
  282. End;
  283.  
  284. { Reads the record pointed to by current. }
  285. Procedure TFreadObj.ReadCurrent(var frec);
  286. Begin
  287.   ReadRec(frec,Current);
  288. End;
  289.  
  290. { Returns the number of records in the file.}
  291. Function TFreadObj.NumRecs:Longint;
  292. Begin
  293.   if not Oerror then
  294.     NumRecs:=Filesize(Ifile);
  295. End;
  296.  
  297. { Returns the file name of the file.}
  298. Function TFreadObj.GetFilename : String;
  299. Begin
  300.   GetFilename:=FileName;
  301. End;
  302.  
  303. { Returns the number of the current record. }
  304. Function TFreadObj.GetCurrent : Longint;
  305. Begin
  306.   GetCurrent:=Current;
  307. End;
  308.  
  309. { No initialization required.}
  310. end.